package com.tarena.vehicle.management.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.vehicle.common.ex.ServiceException;
import com.tarena.vehicle.common.pojo.vo.PageData;
import com.tarena.vehicle.common.util.PageInfoToPageDataConverter;
import com.tarena.vehicle.common.web.ServiceCode;
import com.tarena.vehicle.management.converter.VehicleMaintenanceConverter;
import com.tarena.vehicle.management.mapper.VehicleMaintenanceMapper;
import com.tarena.vehicle.management.mapper.VehicleMapper;
import com.tarena.vehicle.management.pojo.entity.VehicleMaintenance;
import com.tarena.vehicle.management.pojo.param.VehicleMaintenanceAddNewParam;
import com.tarena.vehicle.management.pojo.param.VehicleMaintenanceUpdateParam;
import com.tarena.vehicle.management.pojo.query.VehicleMaintenanceQuery;
import com.tarena.vehicle.management.pojo.vo.VehicleMaintenanceListItemVO;
import com.tarena.vehicle.management.pojo.vo.VehicleMaintenanceStandardVO;
import com.tarena.vehicle.management.service.IVehicleMaintenanceService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class VehicleMaintenanceServiceImpl implements IVehicleMaintenanceService {

    @Autowired
    private VehicleMaintenanceMapper vehicleMaintenanceMapper;

    @Autowired
    private VehicleMapper vehicleMapper;

    @Autowired
    private VehicleMaintenanceConverter vehicleMaintenanceConverter;

    @Transactional
    @Override public void addNew(VehicleMaintenanceAddNewParam param) {

        VehicleMaintenance maintenance = vehicleMaintenanceConverter.param2po(param);
        if (param.getStatus() == 1) {
            vehicleMapper.updateStatusById(param.getVehicleId());
        }
        int row = vehicleMaintenanceMapper.insert(maintenance);
        if (row < 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "新增该车辆维修信息失败，服务器忙，请稍后再尝试！");
        }
    }

    @Override public VehicleMaintenanceStandardVO getVehicleMaintenanceById(Integer id) {
        VehicleMaintenanceStandardVO vehicleMaintenanceStandardVO=vehicleMaintenanceMapper.getVehicleMaintenanceById(id);
        if (vehicleMaintenanceStandardVO == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "该车辆维修信息不存在");
        }
        return vehicleMaintenanceStandardVO;
    }
    @Transactional
    @Override public void updateStatusDone(Integer id) {
        VehicleMaintenanceStandardVO vo = getVehicleMaintenanceById(id);
        VehicleMaintenance maintenance = new VehicleMaintenance();
        maintenance.setId(vo.getId());
        maintenance.setStatus(0);
        vehicleMapper.updateStatusNormalById(vo.getVehicleId());
        int row=vehicleMaintenanceMapper.update(maintenance);
        if (row < 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "修改状态失败，服务器忙，请稍后再尝试！");
        }
    }

    @Override public void update(VehicleMaintenanceUpdateParam param) {
        VehicleMaintenanceStandardVO vehicleMaintenanceStandardVO = vehicleMaintenanceMapper.getVehicleMaintenanceById(param.getId());
        if (vehicleMaintenanceStandardVO == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "该车辆维修信息不存在");
        }
        VehicleMaintenance vehicleMaintenance = vehicleMaintenanceConverter.param2po(param);
        if (param.getStatus()==0&&vehicleMaintenanceStandardVO.getStatus()!=param.getStatus())
            vehicleMapper.updateStatusNormalById(vehicleMaintenanceStandardVO.getVehicleId());
        int row = vehicleMaintenanceMapper.update(vehicleMaintenance);
        if (row < 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "修改失败，服务器忙，请稍后再尝试！");
        }
    }

    @Override public void deleteById(Integer id) {
        VehicleMaintenanceStandardVO vehicleMaintenanceStandardVO = vehicleMaintenanceMapper.getVehicleMaintenanceById(id);
        if (vehicleMaintenanceStandardVO == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "该车辆维修信息不存在");
        }
        int row =vehicleMaintenanceMapper.deleteById(id);
        if (row < 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "删除失败，服务器忙，请稍后再尝试！");
        }
    }

    @Override
    public PageData<VehicleMaintenanceListItemVO> getVehicleMaintenanceList(VehicleMaintenanceQuery query, Integer num, Integer size) {
        if (num != null && size != null)
            PageHelper.startPage(num, size);
        VehicleMaintenance vehicleMaintenance=vehicleMaintenanceConverter.query2po(query);
        List<VehicleMaintenanceListItemVO> list=vehicleMaintenanceMapper.getVehicleMaintenanceList(vehicleMaintenance);
        PageInfo<VehicleMaintenanceListItemVO> info = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(info);
    }
}
